<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

use Think\Controller;

class MenuController extends BaseController {

    /**
     * 自定义菜单列表
     */
    public function menu_diy() {
        $res = M('menu')->where('status = 1')->order('sort asc,menu_id desc')->select();
        $this->assign('menu', $res);
        $this->display();
    }
    /**
     * 自定义菜单添加
     * 
     */
    public function add_menu() {
        if (IS_POST) {
            $menu_id = I('menu_id');
            $map['menu_name'] = I('menu_name');
            $map['pid'] = I('pid');
            $map['type'] = I('type');
            $map['url'] = I('url');
            $map['key'] = I('key');
            $map['sort'] = I('sort') ? I('sort') : 50;
            $map['remak'] = I('remark');
            $map['status'] = '1';
            //$map['add_time'] = time();
            if (empty($map['menu_name']) || empty($map['type'])) {
                $this->error('参数错误...');
            }
            //判断添加的是否是三级菜单，如是则提示错误
            $child = M('menu')->where('menu_id = '.$map['pid'])->getField('pid');
            if(!empty($child)){
               $this->error('错误，只能添加二级菜单！返回...','/Admin/Menu/menu_diy'); 
            }
            if (empty($menu_id)) {
                $sql = M('menu')->data($map)->add();
            } else {
                $sql = M('menu')->where('menu_id = ' . $menu_id)->data($map)->save();
            }

            if ($sql) {
                $this->success('保存成功', '/Admin/Menu/menu_diy');
            }
        } else {
            $menu_id = I('menu_id') ? I('menu_id') : 0;
            $menu_info = M('menu')->where('menu_id = ' . $menu_id)->find();
            $menu_all = M('menu')->where('status = 1')->select();
            $this->assign('menu_all', $menu_all);
            $this->assign('menu_info', $menu_info);
            $this->display();
        }
    }
    /**
     * 创建微信自定义菜单
     * 
     */
    public function create_menu() {
        //获取自定义菜单一级菜单和二级菜单 类型、名称、ID
        $menu = M('menu')->where('pid = 0 and status = 1')->order('sort asc,menu_id desc')->select();
        $newmenu["button"] = array();
        for ($i = 0; $i < count($menu); $i++) {
            if ($menu[$i]['type'] == 'view') {
                $sub = M('menu')->where('pid = ' . $menu[$i]['menu_id'])->order('sort asc, menu_id desc')->select();
                if ($sub) {
                    $sub_button = array();
                    for ($j = 0; $j < count($sub); $j++) {
                        if ($sub[$j]["type"] == "view") {
                            array_push($sub_button, array('type' => 'view', 'name' => urlencode($sub[$j]["menu_name"]), 'url' => $sub[$j]["url"]));
                        } else {
                            array_push($sub_button, array('type' => 'click', 'name' => urlencode($sub[$j]["menu_name"]), 'key' => urlencode($sub[$j]["key"])));
                        }
                    }
                    array_push($newmenu["button"], array('name' => urlencode($menu[$i]["menu_name"]), 'sub_button' => $sub_button));
                } else {
                    array_push($newmenu["button"], array('type' => 'view', 'name' => urlencode($menu[$i]["menu_name"]), 'url' => $menu[$i]["url"]));
                }
            } else {
                $sub = M('menu')->where('pid = ' . $menu[$i]['menu_id'])->order('sort asc, menu_id desc')->select();
                if ($sub) {
                    $sub_button = array();

                    for ($j = 0; $j < count($sub); $j++) {
                        if ($sub[$j]["type"] == "view") {
                            array_push($sub_button, array('type' => 'view', 'name' => urlencode($sub[$j]["menu_name"]), 'url' => $sub[$j]["url"]));
                        } else {
                            array_push($sub_button, array('type' => 'click', 'name' => urlencode($sub[$j]["menu_name"]), 'key' => urlencode($sub[$j]["key"])));
                        }
                    }
                    array_push($newmenu["button"], array('name' => urlencode($menu[$i]["menu_name"]), 'sub_button' => $sub_button));
                } else {
                    array_push($newmenu["button"], array('type' => 'click', 'name' => urlencode($menu[$i]["menu_name"]), 'key' => urlencode($menu[$i]["key"])));
                }
            }
        }
        $wchat = C('weixin');
        import('Common.Library.Https');
        if (!empty($menu)) {
            $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $wchat['Appid'] . "&secret=" . $wchat['AppSecret'];
            $http_handle = \Common\Library\Https::doGet($access_token);
            if ($http_handle) {
                $arr_acc_token = json_decode($http_handle, TRUE); //解析获取access_token
                $del = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $arr_acc_token['access_token'];
                $del_arr = \Common\Library\Https::doGet($del);
                $del_a = json_decode($del_arr, TRUE); //解析获取删除菜单成功信息码

                if ('ok' == $del_a['errmsg']) {
                    $creat = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $arr_acc_token['access_token'];
                    $data = json_encode($newmenu);//生成菜单json
                    $data = urldecode($data);
                    //$data = urlencode($data);
                    $creat_arr = \Common\Library\Https::doPost($creat, $data);
                    $creat_a = json_decode($creat_arr, TRUE); //解析获取创建菜单信息码
                    if ('ok' == $creat_a['errmsg']) {
                        $this->success('新增菜单成功!','/Admin/Menu/menu_diy');
                    }else{
                        $this->error('新增菜单失败!','/Admin/Menu/menu_diy');
                    }
                }
            }
        }
    }
    /**
     * 
     * 删除自定义数据库菜单
     */

    public function del_menu(){
        $menu_id = I('menu_id');
        if(!empty($menu_id)){
            $sql = M('menu')->where('menu_id = '.$menu_id)->setField('status',2);
            if($sql){
                $this->success('菜单删除成功！','/Admin/Menu/menu_diy');
            }else{
                $this->error('菜单删除失败！','/Admin/Menu/menu_diy');
            }
        }
    }
}
